#ifndef BLOCK_DEMO_HPP
#define BLOCK_DEMO_HPP

#include <string>
#include <memory>
#include <iostream>

namespace test {

struct block_t {
    int number;
    std::string string;
};

void test1() {
    block_t* p_block = (block_t*)std::malloc(sizeof(block_t)); // 使用内存块的方式
    block_t b = {1, "haha"}; // 使用对象结构的方式
    std::cout << "before: " << b.number << b.string << std::endl;
    std::memcpy(p_block, &b, sizeof(block_t));
    std::cout << "after: " << p_block->number << p_block->string << std::endl;
    std::free(p_block);
}

void test2() {
    block_t* p_block = (block_t*)std::malloc(sizeof(block_t)); // 使用内存块的方式
    block_t b = {1, "haha"}; // 使用对象结构的方式
    std::cout << "before: " << b.number << b.string << std::endl;
    p_block->number = b.number;
    p_block->string = b.string;
    std::cout << "after: " << p_block->number << p_block->string << std::endl;
    std::free(p_block);
}

void test_block_demo() {
    test2();
}

}

#endif // BLOCK_DEMO_HPP